<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class registro extends CI_Controller {

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		if($this->session->userdata('logged_in')){
			redirect('stock', 'refresh');
		}else{
			$this->load->view('registro_view');
		}
	}

	function reg(){
		if( $this->input->post() ){
			$this->load->model('login_model');
			$this->load->library('usuario');

			$usuario = new usuario();

			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$password2 = $this->input->post('re-password');
			$email = $this->input->post('email');

			if( $password == $password2 ){
				if( !$this->login_model->verificarUsuario($username, $email) ){
					$usuario->setUsu_username($username);
					$usuario->setUsu_password(md5($password));
					$usuario->setUsu_email($email);

					$this->login_model->insertarUsuario($usuario);

					$datos['error'] = 'Usuario creado';
					$this->load->view('login_view', $datos);
				}else{
					$datos['error'] = 'Usuario o Email ya utilizado, intente con otro';
					$this->load->view('registro_view', $datos);
				}
			}else{
				$datos['error'] = 'Las contraseñas no coinciden';
				$this->load->view('registro_view', $datos);
			}
		}else{
			$this->load->view('login_view');
		}
	}

}

/* End of file  */
/* Location: ./application/controllers/ */